package com.qiaofei.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiaofei.mapper.EstateMapper;
import com.qiaofei.pojo.*;
import com.qiaofei.service.EstateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EstateServiceImpl implements EstateService {

    @Autowired
    private EstateMapper estateMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<Estate> estateList = estateMapper.list();
        Page<Estate> p = (Page<Estate>) estateList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult(), p.getPages());
        return pageBean;
    }

    @Override
    public int add(Estate estate) {
        estate.setCreateTime(LocalDateTime.now());
        estate.setUpdateTime(LocalDateTime.now());

        return estateMapper.insert(estate);
    }

    @Override
    public int update(Estate estate) {
        estate.setUpdateTime(LocalDateTime.now());
        return estateMapper.update(estate);
    }

    @Override
    public List<Estate> getByIds(List<Integer> ids) {
        List<Estate> estateList = estateMapper.getByIds(ids);
        return estateList;
    }

    @Override
    public int delete(List<Integer> ids) {
        return estateMapper.delete(ids);
    }

    @Override
    public List<Estate> list() {
        List<Estate> estateList = estateMapper.list();
        return estateList;
    }
}
